package com.leetcode.algorithm.y22.m08.w2;

/**
 * 1417. 重新格式化字符串
 * 
 * https://leetcode.cn/problems/reformat-the-string/
 * 
 * @author jie.deng
 *
 */
class Question1417Solution01 {

	public String reformat(String s) {
		StringBuilder chs = new StringBuilder();
		StringBuilder digits = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			if (ch >= 'a') {
				chs.append(ch);
			} else {
				digits.append(ch);
			}
		}

		StringBuilder str = new StringBuilder();
		if (digits.length() >= chs.length()) {
			if (digits.length() - chs.length() > 1) {
				return "";
			}
			int cnt = 0;
			while (cnt < chs.length()) {
				str.append(digits.charAt(cnt));
				str.append(chs.charAt(cnt));
				cnt++;
			}
			if (cnt < digits.length()) {
				str.append(digits.charAt(cnt));
			}
		} else {
			if (chs.length() - digits.length() > 1) {
				return "";
			}
			int cnt = 0;
			while (cnt < digits.length()) {
				str.append(chs.charAt(cnt));
				str.append(digits.charAt(cnt));
				cnt++;
			}
			str.append(chs.charAt(cnt));
		}
		return str.toString();
	}

}